<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="ID" field="id">
            <a-input v-model="searchForm.id" placeholder="请输入ID" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="用户 ID" field="uid">
            <a-select
              v-model="searchForm.uid"
              :field-names="{ label: 'nickname', value: 'id' }"
              :options="optionData.uid"
              placeholder="请选择用户 ID"
              allow-clear
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="任务订单" field="task_no">
            <a-input v-model="searchForm.task_no" placeholder="请输入任务订单" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="模板 ID" field="template_id">
            <a-select
              v-model="searchForm.template_id"
              :field-names="{ label: 'name', value: 'id' }"
              :options="optionData.template_id"
              placeholder="请选择模板 ID"
              allow-clear
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="是否人工单" field="manual">
            <sa-select v-model="searchForm.manual" dict="yes_or_no" placeholder="请选择是否人工单" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="创建时间" field="create_time">
            <a-range-picker v-model="searchForm.create_time" :show-time="true" mode="date" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="提交状态" field="submission_status">
            <sa-select v-model="searchForm.submission_status" dict="yes_or_no" placeholder="请选择提交状态" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="任务状态" field="task_status">
            <sa-select v-model="searchForm.task_status" dict="task_status" placeholder="请选择任务状态" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="关联服务器名称" field="server_name">
            <a-input v-model="searchForm.server_name" placeholder="请输入关联服务器名称" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="任务完成时间" field="completed_time">
            <a-range-picker v-model="searchForm.completed_time" :show-time="true" mode="date" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="是否分享" field="is_share">
            <sa-select v-model="searchForm.is_share" dict="yes_or_no" placeholder="请选择是否分享" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="检测状态" field="check_status">
            <sa-select v-model="searchForm.check_status" dict="check_status" placeholder="请选择检测状态" allow-clear />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import commonApi from '@/api/common'
import EditForm from './edit.vue'
import api from '../api/task'

// 引用定义
const crudRef = ref()
const editRef = ref()
const optionData = reactive({
  uid: [],
  template_id: [],
})

// 搜索表单
const searchForm = ref({
  id: '',
  uid: '',
  task_no: '',
  template_id: '',
  manual: '',
  create_time: [],
  submission_status: '',
  task_status: '',
  server_name: '',
  completed_time: [],
  is_share: '',
  check_status: '',
})


// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  recycleApi: api.getRecyclePageList,
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ['/ai/Task/save'],
    func: async () => {
      editRef.value?.open()
    },
  },
  edit: {
    show: true,
    auth: ['/ai/Task/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/ai/Task/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
    realAuth: ['/ai/Task/realDestroy'],
    realFunc: async (params) => {
      const resp = await api.realDestroy(params)
      if (resp.code === 200) {
        Message.success(`销毁成功！`)
        crudRef.value?.refresh()
      }
    },
  },
  recovery: {
    show: true,
    auth: ['/ai/Task/recovery'],
    func: async (params) => {
      const resp = await api.recovery(params)
      if (resp.code === 200) {
        Message.success(`恢复成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
  { title: 'ID', dataIndex: 'id', width: 180, sortable: { sortDirections: ['ascend', 'descend'] } },
  { title: '用户 ID', dataIndex: 'uid', width: 180 },
  { title: '任务订单', dataIndex: 'task_no', width: 180 },
  { title: '原图 URL', dataIndex: 'original_img_url', type: 'image', width: 180 },
  { title: '遮罩 URL', dataIndex: 'original_mask_url', type: 'image', width: 180 },
  { title: '模板 ID', dataIndex: 'template_id', width: 180 },
  { title: '清晰度', dataIndex: 'articulation', width: 180 },
  { title: '成品二次加工用途', dataIndex: 'seed', width: 180 },
  { title: '是否人工单', dataIndex: 'manual', dict: 'yes_or_no', width: 180 },
  { title: '人工单描述', dataIndex: 'remarks', width: 180 },
  { title: '创建时间', dataIndex: 'create_time', width: 180 },
  { title: '服务器返回', dataIndex: 'prompt_id', width: 180 },
  { title: '提交状态', dataIndex: 'submission_status', dict: 'yes_or_no', width: 180 },
  { title: '任务状态', dataIndex: 'task_status', dict: 'task_status', width: 180 },
  { title: '尝试次数', dataIndex: 'retry_count', width: 180, sortable: { sortDirections: ['ascend', 'descend'] } },
  { title: '任务结果 URL', dataIndex: 'result_img_url', type: 'image', width: 180 },
  { title: '关联服务器名称', dataIndex: 'server_name', width: 180 },
  { title: '任务完成时间', dataIndex: 'completed_time', width: 180 },
  { title: '是否分享', dataIndex: 'is_share', dict: 'yes_or_no', width: 180 },
  { title: '点赞次数', dataIndex: 'view', width: 180, sortable: { sortDirections: ['ascend', 'descend'] } },
  { title: '检测状态', dataIndex: 'check_status', dict: 'check_status', width: 180 },
  { title: '检测详情', dataIndex: 'check_info', width: 180 },
])

// 页面数据初始化
const initPage = async () => {
  const resp_uid = await commonApi.commonGet('/ai/User/index?saiType=all')
  optionData.uid = resp_uid.data
  const resp_template_id = await commonApi.commonGet('/ai/TemplatePrice/index?saiType=all')
  optionData.template_id = resp_template_id.data
}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
